<?php
namespace Admin\Controller;

class FriendLinkController extends BaseController
{
    public function indexAction()
    {
        $FriendLink = D('FriendLink');
        $condition = array();
		$count = $FriendLink->where($condition)->count();
		$Page = new \Think\Page($count, 50);
		$show = $Page->show();
		$list = $FriendLink->where($condition)->order('sort asc, id asc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		$this->assign('page', $show);
		$this->assign('list', $list);
        $this->display();
    }
    
    public function addAction()
    {
        if (IS_POST) {
            $FriendLink = D('FriendLink');
            $data = $FriendLink->create($_POST['FriendLink']);
            if (false !== $data) {
                $insertId = $FriendLink->add();
                if (false !== $insertId) {
                    $this->success('新增成功', U('FriendLink/index'));
                } else {
                    $this->error('新增失败（失败原因：' . $FriendLink->getError() . '）');
                }
            } else {
                $this->error($FriendLink->getError());
            }
        } else {
            $this->display();  
        }
    }
    
    public function updateAction()
    {
        $FriendLink = D('FriendLink');
        if (IS_POST) {
            $data = $FriendLink->create($_POST['FriendLink']);
            if (false !== $data) {
                if (false !== $FriendLink->save()) {
                    $this->success('更新成功', U('FriendLink/index'));
                } else {
                    $this->error('更新失败（失败原因：' . $FriendLink->getError() . '）');
                }
            } else {
                $this->error($FriendLink->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $friend_link_info = $FriendLink->find($id);
            if (false === $friend_link_info) {
                $this->error('获取信息失败！');
            }
            $this->assign('friend_link_info', $friend_link_info);
            $this->display();  
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $FriendLink = M('FriendLink');
        if (false !== $FriendLink->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    
	public function sortAction() {
		$FriendLink = D('FriendLink');
        $arr_sort = I('post.sort');
		foreach ($arr_sort as $key => $val) {
			$data = array('sort' => intval($val));
			$FriendLink->where('id = ' . $key)->save($data);
		}
		$this->redirect('FriendLink/index');	
	}
    
	/**
     * 编辑器上传处理
     */
	public function uploadJsonAction() 
    {
		$config = array(
			'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'friend_link/',
            'subPath' => 'friend_link/',
			'saveUrl' => C('PICTURE_UPLOAD_URL'),
		);
        vendor('Kindeditor.UploadJson', '', '.class.php');
		$UploadJson = new \UploadJson($config);
		$UploadJson->upload();
	}
    
	/**
     * 编辑器上传文件管理
     */
	public function fileManagerJson()
    {
		
	}
}